﻿using EasySL.Data.Interface;
using System;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Web;

namespace EasySL.Service
{
    public class DALFactory
    {
        private static IDAL instance;
        
        public static IDAL CreateInstance()
        {
            if (instance == null)
            {
                string rootPath = string.Empty;

                if (HttpContext.Current != null)
                {
                    //if web application
                    rootPath = HttpRuntime.BinDirectory;
                }
                else
                {
                    //if remoting application
                    rootPath = string.Empty;
                }
                
                string typeName = ConfigurationManager.AppSettings["DALType"];
                string assemblyName = ConfigurationManager.AppSettings["DALAssembly"];

                Assembly assembly = Assembly.LoadFrom(rootPath + assemblyName);
                instance = assembly.CreateInstance(typeName) as IDAL;
            }

            return instance;
        }
    }
}
